import Vue from 'vue';
import Vuex from 'vuex';
import {getCitys,getWeather} from '@/api/index.js';
import {getOptions,setOptions} from '@/util/index.js';

Vue.use(Vuex);

export default new Vuex.Store({
    state : {
        isLoading : false,
        citysData : [],
        chooseId: {
            providerId: null,
            cityId: null,
            areaId: null,
        },
        weatherData : {},
        indexSelect : []
    },
    mutations : {
        // 修改所有城市信息
        setCityData(state,data){
            state.citysData = data;
        },
        // 设置选择的城市信息
        setCityId (state,data){
            state.chooseId = {...state.chooseId, ...data}
        },
        // 设置开关的数据
        setIndexSelect (state,data){
            state.indexSelect = data;
            setOptions({indexSelect: data});
        },
        // 设置加载状态
        setLoading(state,data){
            state.isLoading = data;
        },
        // 设置天气数据
        setWeatherData (state,data){
            state.weatherData = data;
        }
    },
    actions : {
        // 获取所有城市数据
        async getCityData (context){
            let data = await getCitys();
            context.commit('setCityData',data);
        },
        // 初始化设置信息；
        initOptions (context){
            let options = getOptions() || {};

            let {provinceId,cityId,areaId,indexSelect} = options;
            if(!indexSelect) {
                indexSelect = new Array(7);
                for (let i = 0; i < indexSelect.length; i ++){
                    indexSelect[i] = {value : true}
                }
            }
            context.commit('setIndexSelect', indexSelect);
            context.commit('setCityId',{provinceId,cityId,areaId})
        },
        // 获取天气数据
        async getWetherData (context){
            context.commit('setLoading', true);
            let data = await getWeather({cityid: context.state.chooseId.areaId});
            context.commit('setWeatherData',data);
            context.commit('setLoading', false);
        }
    },
    getters : {

        // 国内的所有省份数据
        provinceArr (store){
            return store.citysData.filter(item=>item.parentid === 0).map(item=>({value: item.cityid,label:item.city}));
        },

        // 当权选择省份的城市数组；
        cityArr(store){
            if( store.isMunicipality) {
                return  null;
            }
            return store.citysData.filter(item=>item.parentid === store.chooseId.provinceId).map(item=>({value: item.cityid,label:item.city}));
        },

        // 当前选择城市的区域数组；
        areaArr(store){
            return store.citysData.filter(item=>item.parentid === store.chooseId.cityId).map(item=>({value: item.cityid,label:item.city}));
        },

        // 判断是否是直辖市
        isMunicipality(store){
            return store.chooseId.provinceId === 1 || store.chooseId.provinceId === 24;
        },

        // 省份名称
        providerName (store){
            return store.citysData.find(item=>item.cityid === store.chooseId.provinceId).city;
        },
        // 城市名称
        cityName (store){
            try{
                return store.citysData.find(item=>item.cityid === store.chooseId.cityId).city;
            } catch (e) {
                return '';
            }
        },
        // 区域名称
        areaName (store){
            try {
                return store.citysData.find(item=>item.cityid === store.chooseId.areaId).city;
            }catch (e) {
                return '';
            }
        }
    }
});
